package cn.finder.datatunnel;

import java.io.IOException;

import org.apache.log4j.Logger;

import cn.finder.datatunnel.processor.Processor;

public class ProcessorRunnable implements Runnable{

	private static Logger logger=Logger.getLogger(ProcessorRunnable.class);
	
	private Processor processor;
	
	public ProcessorRunnable(Processor processor){
		this.processor=processor;
	}

	public void run() {
		
		byte[] data1;
		try {
			
			data1 = processor.read();
			if(processor.getChain()){
				data1=processor.afterRead(data1);
				if(processor.getChain()){
					data1=processor.beforeSend(data1);
					
					if(processor.getChain()){
						byte[] data2=processor.send(data1);
						
						if(processor.getChain()){
							data2=processor.afterSend(data2);
							if(processor.getChain()){
								
								data2=processor.beforeWrite(data2);
								
								if(processor.getChain()){
									
									String writeResult=processor.write(data2);
									if(processor.getChain()){
										processor.afterWrite(writeResult);
									}
								}
							}
						}
					}
				}
			}
			
			
		}
		catch(InfoException infoExp){
			logger.info(infoExp.getMessage());
		}
		catch (IOException e) {
			logger.error(e);
		}
		catch(DTException exp){
			logger.error(exp);
		}
		
		
	}
	
	
}
